﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;

namespace TULT
{
    public partial class FrmMain : Form
    {
        readonly ChampionSelectApi _api = new ChampionSelectApi();
        readonly XmlDocument database = new XmlDocument();

        public FrmMain()
        {
            InitializeComponent();
            if (File.Exists("Database.xml"))
            {
                database.Load(Application.StartupPath + "\\Database.xml");
                XmlNodeList champNames = database.GetElementsByTagName("name");
                for (int i = 0; i < champNames.Count; ++i)
                {
                    lstChamps.Items.Add(champNames[i].InnerText);
                }
                // To prevent autoselection first item in the list and loading it...
                lstChamps.SelectionMode = SelectionMode.One;
            }
        }

        private void lstChamps_SelectedIndexChanged(object sender, EventArgs e)
        {            
            UpdateChampInfo();
        }

        private void UpdateChampInfo()
        {
            // show name
            lblName.Text = string.Format("{0}\n{1}", database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[0].InnerText, database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[1].InnerText);
            // show icon
            imgChamp.Image = BitmapFromByteArray(Convert.FromBase64String(database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[2].InnerText));
            // get countermechanics and show in list
            XmlNodeList counterMechanics = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[3].ChildNodes;
            lstGenCounter.Items.Clear();
            for (int i = 0; i < counterMechanics.Count; i++)
            {
                lstGenCounter.Items.Add(counterMechanics[i].InnerText);
            }

            XmlNodeList badAgainst = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[5].ChildNodes;
            lstBadAgainst.Items.Clear();
            for (int i = 0; i < badAgainst.Count; i++)
            {
                if (lstBadAgainst.Items.Contains(badAgainst[i].InnerText) == false)
                lstBadAgainst.Items.Add(badAgainst[i].InnerText);
            }

            XmlNodeList goodAgainst = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[6].ChildNodes;
            lstGoodAgainst.Items.Clear();
            for (int i = 0; i < goodAgainst.Count; i++)
            {
                if (lstGoodAgainst.Items.Contains(badAgainst[i].InnerText) == false)
                lstGoodAgainst.Items.Add(goodAgainst[i].InnerText);
            }

            XmlNodeList counterItems = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[4].ChildNodes;
            lstCounterItems.Items.Clear();
            for (int i = 0; i < counterItems.Count; i++)
            {
                lstCounterItems.Items.Add(counterItems[i].InnerText);
            }

            XmlNodeList counterSpells = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[12].ChildNodes;
            lstCounterSpells.Items.Clear();
            for (int i = 0; i < counterSpells.Count; i++)
            {
                lstCounterSpells.Items.Add(counterSpells[i].InnerText);
            }

            XmlNodeList classes = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[8].ChildNodes;
            lstClasses.Items.Clear();
            for (int i = 0; i < classes.Count; i++)
            {
                lstClasses.Items.Add(classes[i].InnerText);
            }

            XmlNodeList damagetype = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[9].ChildNodes;
            lstDamageType.Items.Clear();
            for (int i = 0; i < damagetype.Count; i++)
            {
                lstDamageType.Items.Add(damagetype[i].InnerText);
            }

            XmlNodeList role = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[10].ChildNodes;
            lstRole.Items.Clear();
            for (int i = 0; i < role.Count; i++)
            {
                lstRole.Items.Add(role[i].InnerText);
            }

            XmlNodeList stats = database.ChildNodes[1].ChildNodes[lstChamps.SelectedIndex].ChildNodes[11].ChildNodes;
            lblHP.Text = stats[0].InnerText;
            lblMP.Text = stats[1].InnerText;
            lblDamage.Text = stats[2].InnerText;
            lblAR.Text = stats[3].InnerText;
            lblArmor.Text = stats[4].InnerText;
            lblHR.Text = stats[5].InnerText;
            lblManaReg.Text = stats[6].InnerText;
            lblAS.Text = stats[7].InnerText;
            lblMS.Text = stats[8].InnerText;
            lblMR.Text = stats[9].InnerText;

            //imgChamp.Image = BitmapFromByteArray(Convert.FromBase64String(_api.ChampionImageInAscii));
            //lstGenCounter.DataSource = _api.CounterMechanics;
        }

        private void btnGenDatabase_Click(object sender, EventArgs e)
        {
            List<Champion> championList = new List<Champion>();
            foreach (string champion in lstChamps.Items)
            {
                _api.SetChampion = champion;
                Champion champ = new Champion();
                Stats stats = new Stats();
                champ.Name = _api.SetChampion;
                champ.SubName = _api.SubName;
                champ.IconAsString = _api.ChampionImageInAscii;
                champ.CounterMechanics = _api.CounterMechanics;
                champ.CounterItems = _api.CounterItems;
                champ.CounterSummSpells = _api.CounterSummonerSpells;
                champ.BadAgainst = _api.BadAgainst;
                champ.GoodWith = _api.GoodWith;
                champ.GoodAgainst = _api.GoodAgainst;
                champ.Class = _api.GetClass;
                champ.DamageType = _api.GetDamageType;
                champ.Role = _api.GetRole;
                stats.HealthPoints = _api.GetHealthPoints;
                stats.ManaPoints = _api.GetManaOrEnergyPoints;
                stats.Damage = _api.GetDamage;
                stats.AttackRange = _api.GetAttackRange;
                stats.Armor = _api.GetArmor;
                stats.HealthRegen = _api.GetHealthRegen;
                stats.ManaRegen = _api.GetManaOrEnergyRegen;
                stats.AttackSpeed = _api.GetAttackSpeed;
                stats.MovementSpeed = _api.GetMovementSpeed;
                stats.MagicResistance = _api.GetMagicResistance;
                champ.Stats = stats;
                championList.Add(champ);
            }
            WriteDatabase(championList);
            database.Load(Application.StartupPath + "\\Database.xml");
        }

        private Bitmap BitmapFromByteArray(byte[] byteArray)
        {
            MemoryStream dataStream = new MemoryStream(byteArray, 0, byteArray.Length);
            Bitmap bitTemp = (Bitmap)Image.FromStream(dataStream);
            dataStream.Close();
            return bitTemp;
        }

        private void WriteDatabase(List<Champion> _champions)
        {
            XElement champions =
                new XElement("champions",
                             from s in _champions
                             select new XElement("champion",
                                                 new XElement("name", s.Name),
                                                 new XElement("subname", s.SubName),
                                                 new XElement("icon", s.IconAsString),
                                                 new XElement("countermechanics",
                                                              from mechanic in s.CounterMechanics
                                                              select new XElement("mechanic", mechanic)),
                                                 new XElement("counteritems",
                                                              from item in s.CounterItems
                                                              select new XElement("item", item)),
                                                 new XElement("badagainst",
                                                              from badAgainst in s.BadAgainst
                                                              select new XElement("champion", badAgainst)),
                                                 new XElement("goodagainst",
                                                              from goodAgainst in s.GoodAgainst
                                                              select new XElement("champion", goodAgainst)),
                                                 new XElement("goodwith",
                                                              from goodWith in s.GoodWith
                                                              select new XElement("champion", goodWith)),
                                                 new XElement("classes",
                                                              from classType in s.Class
                                                              select new XElement("class", classType)),
                                                 new XElement("damagetype",
                                                              from damageType in s.DamageType
                                                              select new XElement("class", damageType)),
                                                 new XElement("role",
                                                              from roleType in s.Role
                                                              select new XElement("class", roleType)),
                                                 new XElement("stats",
                                                         new XElement("healthpoints", s.Stats.HealthPoints),
                                                         new XElement("manapoints", s.Stats.ManaPoints),
                                                         new XElement("damage", s.Stats.Damage),
                                                         new XElement("attackrange", s.Stats.AttackRange),
                                                         new XElement("Armor", s.Stats.Armor),
                                                         new XElement("healthregen", s.Stats.HealthRegen),
                                                         new XElement("manaregen", s.Stats.ManaRegen),
                                                         new XElement("attackspeed", s.Stats.AttackSpeed),
                                                         new XElement("movementspeed", s.Stats.MovementSpeed),
                                                         new XElement("magicresistance", s.Stats.MagicResistance)
                                                         ),
                                                new XElement("counterspells",
                                                            from summSpell in s.CounterSummSpells
                                                            select new XElement("spell", summSpell))
                                 )
                    );
            champions.Save("Database.xml");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            imgWards.Image = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("TULT.Resources.botlanewarding.png"));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            imgWards.Image = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("TULT.Resources.lategamewarding.jpg"));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            imgWards.Image = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("TULT.Resources.midwarding.jpg"));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ChampionSelectApi kaas = new ChampionSelectApi();
            kaas.SetChampion = "Kennen";
            MessageBox.Show(kaas.GetManaOrEnergyRegen);
        }
        
    }
}
